<?php
//自动生成,请根据需要修改
namespace app\login\controller;
use app\common\controller\Base;
use function MongoDB\BSON\toJSON;
use phpDocumentor\Reflection\Types\Object_;
use think\Db;
use EasyWeChat\Factory;
use think\Exception;
use think\Validate;
use tools\curl\Curl;

/**
 * @title 登录页面（模块04-02）
 * @description 登录说明
 * @group 登录
 * @header name:model_id require:0 default:02-02 desc:模块
 * @groupremark 已使用错误码：1-04-02-0001 校验失败 1-04-02-0002 添加失败 1-04-02-0003 编辑校验失败 1-04-02-0004 编辑失败 1-04-02-0005 获取详情失败  1-04-02-0006 获取列表失败 1-04-02-0007获取单个详情失败 1-04-02-0008 删除失败
 */
class Login extends Base
{
    /**
     * @title 用户登录
     * @description 用户登录
     * @author 宋晓文
     * @url /login/login/login
     * @method POST
     * @param name:username type:int require:1 default:1 other: desc:账号
     * @param name:password type:int require:1 default:1 other: desc:密码
     * @return data:帐号信息@!
     * @data token:token
     *
     */
    public function login()
    {
        $data = input();
        //验证器
        $validate = new Validate([
            'username' => 'require',
            'password' => 'require',
        ], [
            'username' => '账号不能为空',
            'password' => '密码不能为空',
        ]);
        if (!$validate->check($data)) {
            $msg['msg'] = $validate->getError();
            return $this->errorJson($msg);
        }
        //获取账号信息
        $account = Db::name('account')->where('account_name',$data['username'])->find();
        //判断账号和密码是否正确
        if(!$account || $account['password']!==md5($data['password'])){
            $msg['msg'] = '账号或密码不正确';
            return $this->errorJson($msg);
        }
        //判断是否被禁用
        if ($account['islock'] == 1) {
            $msg['msg'] = '该账号用户已被锁定，暂无法使用';
            return $this->errorJson($msg);
        }
        //生成并保存token
        $token_str = save_token($account['id']);
        //登录成功
        $msg['msg'] = '登录成功';
        $msg['token'] = $token_str;
        return $this->successJson($msg);
    }
}
